package com.huopaolan.yohuoshop.Mvp.Presenter;

import android.util.Base64;
import android.util.Log;

import com.blankj.utilcode.util.EncryptUtils;
import com.huopaolan.lib_core.Mvp.Presenter.BasePresenter;
import com.huopaolan.yohuoshop.Bean.UserEntity;
import com.huopaolan.yohuoshop.Bean.UserRegisterEntity;
import com.huopaolan.yohuoshop.Mvp.Contract.UserContract;

import org.json.JSONException;
import org.json.JSONObject;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class RegisterPresenter extends BasePresenter<UserContract.RegisterIView, UserContract.RegisterIModel> {
    private String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJa4C5IKvNRcLWXiLFcF4F+i1S2QAusCMszlQeJV84UetEkczjUdJ4d" +
            "WbnpRkeAmXCTzRHyO67XKS6GSCuKVO/sf7cyll0i6e+d0MSWB2CTxojYingZSV6ZQO8K1Z3fJyFYSHiRhDwJ4idC80zTyKagsWV2" +
            "9uNa38iQYr4FwbNqZAgMBAAECgYAxV1k6W1eMMg0OsKeRabQVuwoNG3tJEnQtDdSu0zKg3vdohAyh6MR7" +
            "EvmiA7g86HH8CsPd/y/9WJe/8j6sBO0Ye9gt7eyQ2NiwWvlTuwNmngcSTapVvVI6NEyJFMfQt9PB1EHLNAXlz8jtJUy" +
            "A7C48jReQD9p/SzAP0VxG7lwyMQJBAOjE7hAZ/6fyP3DB1fG7jr9gONZcz3TUaqx6BUn4GKZnckW08ht9Xqcqft5Hthu8BbLM9" +
            "ptQ0U8QZekrJwD6ya0CQQClwstZMPu8jLhsgugVwodcG1mPEOiw9Yjnmt9+WTI07Ll2uFv//hRXBnahBBnZbucUYEbUY3kqUX9b3e9Tm" +
            "EodAkEAybPMbxt4VDoxCy6Mi/pxChkBZ4/pHV3sSiU6bAyWn6vIc+sGWRfca5MBePA/N+1IKtY9Y/02QwL8rH5+P/URyQJAL/hdjORGFdzLimuf6pwv" +
            "PBKWKncEQCHuisghIZmClBpl2duklELddAnkztg2+tvDd/wcw14+NGb9aoKhvhl2aQJAbvcgoPU+xs0CjeexH+TS2S/jKkTRpvP2CpPK/k71m13xWdE8RtM" +
            "kYY1measRmlIwOfWze7ll/PGT4dxWf31FNg==";

    @Inject
    public RegisterPresenter(UserContract.RegisterIView registerIView, UserContract.RegisterIModel registerIModel) {
        super(registerIView, registerIModel);
    }

    /**
     * 用户注册接口
     */
    public void RegisterRequest(String username, String password) {
        String pwd = password;
        //rsa加密
        byte[] buff = EncryptUtils.encryptRSA(pwd.getBytes(),
                Base64.decode(privateKey.getBytes(), Base64.DEFAULT),
                false, "RSA/ECB/PKCS1Padding");
        //base64
        String pwdStr = Base64.encodeToString(buff, Base64.DEFAULT);
        Log.e("ZXY", "pwdStr:" + pwdStr);
        JSONObject register = new JSONObject();
        try {
            register.put("username", username);
            register.put("password", pwdStr);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String request = register.toString();
        m.register_user(request).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserRegisterEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UserRegisterEntity entity) {
                        v.RegisterSuccess(entity);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
